package com.app.Cutofftime;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.app.Cutofftime.Database.DatabaseHelper;
import com.google.android.material.textfield.TextInputLayout;

import java.util.regex.Pattern;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;


public class SignupActivity extends AppCompatActivity {

    private DatabaseHelper helper;
    private EditText input_user,input_password,input_password2;
    private Button signup;
    private TextInputLayout passwordLayout, nameLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);

        Toolbar toolbar = findViewById(R.id.bar2);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        input_user = findViewById(R.id.user);
        input_password = findViewById(R.id.password);
        input_password2 = findViewById(R.id.password2);
        signup    = findViewById(R.id.button_signup);
        helper    = new DatabaseHelper(getApplicationContext());

        nameLayout     = findViewById(R.id.nameLayout);
        passwordLayout = findViewById(R.id.passwordLayout);

        nameLayout.requestFocus();




        signup.setOnClickListener(v -> {
            String userName = input_user.getText().toString().trim();
            String password = input_password.getText().toString();
            String password2 = input_password2.getText().toString();

            if (input_user.length() == 0) {
                showError(nameLayout, "User cannot be empty");
                passwordLayout.setErrorEnabled(false);
            }
            else if(password.equals(password2)==false){
                showError(passwordLayout, "Two passwords do not equal!");
                nameLayout.setErrorEnabled(false);
            }
            else if(input_password.length() == 0) {
                showError(passwordLayout, "Password cannot be empty");
                nameLayout.setErrorEnabled(false);
            }
            else if(input_user.getText().toString().contains("@")==false) {
                showError(nameLayout, "User must be a email address");
                passwordLayout.setErrorEnabled(false);
            }
            else if (helper.checkUser(userName)) {
                showError(nameLayout, "User already taken");
                passwordLayout.setErrorEnabled(false);
            }
            else if(password.length() < 4) {
                showError(passwordLayout, "Password must contain at least 4 characters");
                nameLayout.setErrorEnabled(false);
            }
            else if(!Pattern.compile("^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{4,15}$").matcher(password).matches()) {
                showError(passwordLayout, "password must contain at least 1 lowercase, 1 uppercase letter, a digit and a symbol");
                nameLayout.setErrorEnabled(false);
            }
            else {
                if(helper.addUser(userName, password)){
                    Toast.makeText(getApplicationContext(),"Signed up successfully",Toast.LENGTH_SHORT).show();
                    nameLayout.setErrorEnabled(false);
                    passwordLayout.setErrorEnabled(false);
                    Intent i = new Intent(getApplicationContext(), WelcomeActivity.class);
                    startActivity(i);
                }
                else {
                    Toast.makeText(getApplicationContext(),"Error: Please try again",Toast.LENGTH_SHORT).show();
                }
            }
        });



        input_password.setOnClickListener(v -> {
            passwordLayout.setErrorEnabled(false);
        });
    }

    public void showError(TextInputLayout view, String errorMsg) {
        view.setError(errorMsg);
        view.requestFocus();
    }

    @Override
    protected void onRestart() {
        input_user.setText("");
        input_password.setText("");
        nameLayout.requestFocus();
        super.onRestart();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        finish();
        return true;
    }
}